local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local RankDefine = require("rank.RankDefine")
local Msg = require("core.Msg")
local BaowuConfig = require("excel.Baowu").baowu
local Log = require("common.Log")
local PfLogic = require("tencentApi.PfLogic")

BAOWU_RANK = BAOWU_RANK or {}
--BAOWU_RANK = {index = {uuid, name, baowuName, baowuStar, baowuPower, create = os.time()}}

function setRankList(human, list, index)
  list.type = RankDefine.BAOWU_RANK_TYPE
  list.name = BAOWU_RANK[index].name
  list.uuid = BAOWU_RANK[index].uuid
  list.baowuName = BAOWU_RANK[index].baowuName
  list.baowuStar = BAOWU_RANK[index].baowuStar
  list.baowuPower = BAOWU_RANK[index].baowuPower
  if PfLogic.isBlueShow(human) then
    list.blue = BAOWU_RANK[index].blue
  else
    list.blue = "0000"
  end
  list.args = 0
end

function GetRankList(human, sendMsg)
  sendMsg.baowulist[0] = #BAOWU_RANK
  for i=1, #BAOWU_RANK do
    if BAOWU_RANK[i].uuid == human._id then
      sendMsg.myRank = i
    end
    setRankList(human, sendMsg.baowulist[i], i)
  end 
  Msg.send(sendMsg, human.fd)
end

function getFirst(human, list)  
  list[0] = 0 
  if BAOWU_RANK[1] then
    list[0] = list[0] + 1
    setRankList(human, list[list[0]], 1)
  end
end

function getBaowuInfo(human)
  local tmp = {}
  local baowuPower = 0
  local power = 0
  for i = 0, human.db.baowuLen -1 do
    if baowuPower <= human.db.baowu[i].power then
      tmp.baowuStar = human.db.baowu[i].starLv
      tmp.baowuName = BaowuConfig[human.db.baowu[i].id].mingzi
      baowuPower = human.db.baowu[i].power      
    end
    power = power + human.db.baowu[i].power
  end
  tmp.baowuPower = power
  return tmp
end

function setRankInfo(human, index, baowuInfo)
  BAOWU_RANK[index] = {}
  BAOWU_RANK[index].uuid = human._id  
  BAOWU_RANK[index].baowuName = baowuInfo.baowuName
  BAOWU_RANK[index].name = human.name
  BAOWU_RANK[index].baowuStar = baowuInfo.baowuStar
  BAOWU_RANK[index].baowuPower = baowuInfo.baowuPower
  BAOWU_RANK[index].account = human.account
  BAOWU_RANK[index].create = os.time()
  BAOWU_RANK[index].blue = string.format("%d%d%d%d",human.db.blueVipLv, human.db.blueYearVip, human.db.blueSuperVip, human.db.level3366)
end

function sortFunc(a, b)
  if a.baowuPower == b.baowuPower then
    return a.create < b.create    
  end
  return a.baowuPower > b.baowuPower
end

function isInRank(human, baowuInfo)
  for i=1, #BAOWU_RANK do
    if BAOWU_RANK[i].uuid == human._id then
      if baowuInfo.baowuPower ~= BAOWU_RANK[i].baowuPower then
        setRankInfo(human, i, baowuInfo)
        table.sort(BAOWU_RANK, sortFunc)
      end
      return true
    end
  end
  return false
end

--外部调用，神将战力变更的时候调用
function baowuPowerChange(human)
  if human.db.lv < 35 then return end
  local baowuInfo = getBaowuInfo(human)
  if isInRank(human, baowuInfo) then
    return
  end
  if human.db.baowuLen <= 0 then
    return
  end
  if #BAOWU_RANK < RankDefine.RANK_MAX_CNT then
    setRankInfo(human, #BAOWU_RANK + 1, baowuInfo)
  elseif #BAOWU_RANK >= RankDefine.RANK_MAX_CNT and baowuInfo.baowuPower > BAOWU_RANK[#BAOWU_RANK].baowuPower then
    setRankInfo(human, RankDefine.RANK_MAX_CNT, baowuInfo)
  else
    return false
  end
  table.sort(BAOWU_RANK, sortFunc)
end

function getRank()
  local dbType = {type = RankDefine.BAOWU_RANK_TYPE}
  LuaMongo.find(DB.db_rank, dbType)
  while true do
    local tmp = {}
    if not LuaMongo.next(tmp) then
      break
    end
    BAOWU_RANK[tmp.index] = {}
    BAOWU_RANK[tmp.index].uuid = tmp.uuid
    BAOWU_RANK[tmp.index].name = tmp.name
    BAOWU_RANK[tmp.index].baowuStar = tmp.baowuStar
    BAOWU_RANK[tmp.index].baowuName = tmp.baowuName
    BAOWU_RANK[tmp.index].baowuPower = tmp.baowuPower
    BAOWU_RANK[tmp.index].account = tmp.account
    BAOWU_RANK[tmp.index].create = tmp.create or os.time()
    BAOWU_RANK[tmp.index].blue = tmp.blue or "0000"
  end
end

function wrapperBaowuData(index)
  local db = {}
  db.type = RankDefine.BAOWU_RANK_TYPE
  db.index = index
  db.uuid = BAOWU_RANK[index].uuid
  db.name = BAOWU_RANK[index].name
  db.baowuName = BAOWU_RANK[index].baowuName
  db.baowuPower = BAOWU_RANK[index].baowuPower
  db.baowuStar = BAOWU_RANK[index].baowuStar
  db.account = BAOWU_RANK[index].account
  db.create = BAOWU_RANK[index].create or os.time()
  db.blue = BAOWU_RANK[index].blue or "0000"
  return db
end

function saveRank()
  for i=1, #BAOWU_RANK do
    local db = wrapperBaowuData(i)
    LuaMongo.insert(DB.db_rank, db)
  end
end

function writeLog()
  for i=1, #BAOWU_RANK do
    Log.write(Log.LOGID_OSS_BAOWU_RANK, BAOWU_RANK[i].uuid, BAOWU_RANK[i].account, BAOWU_RANK[i].name, BAOWU_RANK[i].baowuPower, i)
  end
end